﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace M3U8下载神器
{
    public class SimpleDownloader
    {
        public static string Download(string uri,int times=1)
        {

            for (int i = 0; i < times; i++)
            {
                try
                {
                    var _httpClient = new WebClient();
                    _httpClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36");
                    return _httpClient.DownloadString(uri);
                }
                catch (Exception)
                {

                }
            }
            return null;
        }
        public static string Download2(string uri, int times = 1)
        {

            for (int i = 0; i < times; i++)
            {
                try
                {
                    HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
                    req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36";
                    req.Method = "GET";
                    req.Timeout = 3000;
                    req.ReadWriteTimeout = 3000;
                    using (var stream = req.GetResponse().GetResponseStream())
                    {
                        var ms =new  MemoryStream();
                        stream.CopyTo(ms);
                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
                catch (Exception)
                {

                }
            }
            return null;
        }
    }
}
